import requests
import base64
import urllib

# 获取token
res = requests.get('https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=4jvGHt81mnZ48nnGH0lwT0bG&client_secret=72YiaT9GX0wwWfMUGSDl8FdUfP7Y1Sg4')

# print(res.json())

token = res.json()['access_token']
# print(token)

# 开始智能识图
# 接口地址
url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic?access_token='+token
# 定义头部信息
myheaders = {'Content-Type': 'application/x-www-form-urlencoded'}

# 操作图片
# 读取图片
myimg = open('./code1.png', 'rb')
temp_img = myimg.read()
# 关闭图片
myimg.close()

# 进行base64编码
temp_data = {'image': base64.b64encode(temp_img)}

# 对图片地址进行urlencode操作
temp_data = urllib.parse.urlencode(temp_data)

# 请求识图接口
res = requests.post(url=url, data=temp_data, headers=myheaders)
print(res.json())
code = res.json()['words_result'][0]['words']

code = str(code).replace(' ', '')
print(code)
